package com.yami.shop.bean.serializer.json;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yami.shop.bean.app.dto.FaceDesignConfigDto;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * FaceDesignConfigDto JSON反序列化器
 * @author AI Assistant
 */
@Slf4j
public class FaceDesignConfigDeserializer extends JsonDeserializer<FaceDesignConfigDto> {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public FaceDesignConfigDto deserialize(JsonParser p, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {

        String jsonString = p.getValueAsString();

        if (jsonString == null || jsonString.trim().isEmpty()) {
            return null;
        }

        try {
            // 将JSON字符串反序列化为FaceDesignConfigDto对象
            return objectMapper.readValue(jsonString, FaceDesignConfigDto.class);
        } catch (JsonProcessingException e) {
            log.error("FaceDesignConfigDto反序列化失败: {}, JSON: {}", e.getMessage(), jsonString, e);
            return null;
        }
    }
}
